package com.cn.pretty.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cn.pretty.consts.Constants;
import com.cn.pretty.pojo.FansPojo;
import com.cn.pretty.service.UserService;

@Controller
public class UserController {

	@Resource
	private UserService userService;
	
	public static void main( String[] argn ){
		final String json = "{\"total\":1,\"count\":1,\"data\":{\"openid\":[\"oJx5Ct7b19pgfeYAz693UWF0i-bM\"]},\"next_openid\":\"oJx5Ct7b19pgfeYAz693UWF0i-bM\"}";
		FansPojo fans;
		try {
			fans = Constants.mapper.readValue( json, FansPojo.class );
			if( fans != null ){
				System.out.println( "getTotal : " + fans.getTotal() );
				System.out.println( "getCount : " + fans.getCount() );
				System.out.println( "getNext_openid : " + fans.getNext_openid() );
			}
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		final FansPojo pojo = new FansPojo();
		pojo.setCount( 100 );
		pojo.setNext_openid( "next" );
		pojo.setTotal( 1000 );
		final Map<String,String[]> maps = new HashMap<String,String[]>();
		final String[] openids = {"OPENID1","OPENID2","OPENID3"};
		maps.put( "openid", openids );
		pojo.setData( maps );
		try {
			final String jsons = Constants.mapper.writeValueAsString( pojo );
			System.out.println( "json : " + jsons );
		} catch (JsonGenerationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	@RequestMapping(value = "/user/list", method = RequestMethod.GET)
	public String defult( final HttpServletRequest request, final HttpServletResponse response ){
		final String users = this.userService.getUserList();
		try {
			final String json = "{\"total\":1,\"count\":1,\"data\":{\"openid\":[\"oJx5Ct7b19pgfeYAz693UWF0i-bM\"]},\"next_openid\":\"oJx5Ct7b19pgfeYAz693UWF0i-bM\"}";
			final FansPojo fans = Constants.mapper.readValue( json, FansPojo.class );
			request.setAttribute( "fans", fans );
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute( "users", users );
		return "/users";
	}
}
